#include <direct.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "AtlasBase.h"

namespace Atlas
{

	class CAtlasExeDirInit
	{
	public:
		CAtlasExeDirInit()
		{
#ifdef WIN32
			if(GetModuleFileNameA(NULL, path, sizeof(path)))
			{
				char curpath[400];
				*(strrchr(path, '\\')+1) = '\0';
				ATLAS_VERIFY(getcwd(curpath, sizeof(curpath)));
				ATLAS_VERIFY(chdir(path)==0);
				ATLAS_VERIFY(getcwd(path, sizeof(path)));
				ATLAS_VERIFY(chdir(curpath)==0);
			}
			else
#endif
			{
#ifndef WIN32
				int	res = readlink("/proc/self/exe", path, size);  
				if(res>=0 && res<size)
				{
					path[res] = '\0';
					*(strrchr(path, '\\')+1) = '\0';
				}
				else
#endif
				{
					char curpath[400];
					ATLAS_VERIFY(getcwd(curpath, sizeof(curpath)));
					char exepath[300];
					strcpy(exepath, __argv[0]);
					char* pos1 = strrchr(exepath, '\\');
					char* pos2 = strrchr(exepath, '/');
					if(pos1<pos2) pos1 = pos2;
					if(!pos1)
					{
						strcpy(path, curpath);
					}
					else
					{
						*pos1 = '\0';
						chdir(exepath);
						ATLAS_VERIFY(getcwd(path, sizeof(path)));
						chdir(curpath);
					}
				}
			}

			size_t slen = strlen(path);
			ATLAS_ASSERT(slen>0);
			if(path[slen-1]!='\\' && path[slen-1]!='/')
			{
				path[slen] = ATLAS_PATH_SLASH;
				path[slen+1] = '\0';
			}
		}
		char path[400];
	};
	static CAtlasExeDirInit __global_exe_dir_init;
	const char* GetExeDir()
	{
		return __global_exe_dir_init.path;
	}

}
